package de.akad.imp03.example;

import java.io.IOException;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
public class Servlet extends HttpServlet {
 @Override
 public void doPost(HttpServletRequest req, HttpServletResponse resp)  throws IOException {

  doGet(req,resp);
}
 @Override
 public void doGet(HttpServletRequest req, HttpServletResponse resp)
 throws IOException {
   resp.setContentType("text/html");

   String name = req.getParameter("unternehmen");
   String action = req.getParameter("action");
   StoreHelper helper = new StoreHelper();

   if ("add".equals(action)) {
     helper.addName(name);
     resp.getWriter().println("<html><head><title>Value stored.</title></head><body>");
     resp.getWriter().println("<h1>Value stored.</h1>");
     resp.getWriter().println("<p><a href=\"/planspiel_web20/store\">Home</a>&nbsp;&nbsp;-&nbsp;&nbsp;");
     resp.getWriter().println("<a href=\"/planspiel_web20/store?action=show\">Werte anezeigen.</a></p>");
     resp.getWriter().println("</body></html>");
   } else if ("show".equals(action)) {
	   	 String[] result = helper.getNamen();
	     resp.getWriter().println("<html><head><title>Stored values.</title></head><body>");
	     resp.getWriter().println("<h1>Stored values.</h1>");
	     for (int i=0; i < result.length; i++) {
	    	 resp.getWriter().println("<p>"  + result[i] + "</p>");
	     }
	     resp.getWriter().println("<p><a href=\"/planspiel_web20/store\">Home</a></p>");
	     resp.getWriter().println("</body></html>");
   } else {
	     resp.getWriter().println("<html><head><title>Input Values.</title></head><body>");
	     resp.getWriter().println("<h1>Input values.</h1>");
	     resp.getWriter().println("<form method=\"post\" action=\"/planspiel_web20/store?action=add\">");
	     resp.getWriter().println("<table><tr><td>Name des Unternehmens:</td><td><input name=\"unternehmen\" type=\"text\" length=\"50\"></td></tr></table>");
	     resp.getWriter().println("<button type\"submit\" name=\"Senden\">Senden</button>");
	     resp.getWriter().println("</form>");
	     resp.getWriter().println("</body></html>");
   }
 }
}